﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using tenpay;
using System.Text;
public partial class Pages_buy_tenpay_Tenpay_Return : NoTheme
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        //密钥
        string key = "a788fb0d0c15259488fe26cb397ee2af";
        //创建PayResponseHandler实例
        PayResponseHandler resHandler = new PayResponseHandler(Context);

        resHandler.setKey(key);

        string orderNum = resHandler.getParameter("sp_billno");//本地订单号

        //判断签名
        if (resHandler.isTenpaySign())
        {
            //交易单号
            string transaction_id = resHandler.getParameter("transaction_id");

            //金额金额,以分为单位
            string total_fee = resHandler.getParameter("total_fee");

            //支付结果
            string pay_result = resHandler.getParameter("pay_result");

          

            if ("0".Equals(pay_result))
            {
                //------------------------------
                //处理业务开始
                //------------------------------ 

                //注意交易单不要重复处理
                //注意判断返回金额

                //------------------------------
                //处理业务完毕
                //------------------------------

                //调用doShow, 打印meta值跟js代码,告诉财付通处理成功,并在用户浏览器显示$show页面.
                try
                {
                    string buyerEmail = Request.QueryString["buyer_email"];
                    string tradeNo = transaction_id;
                    DateTime paydate = DateTime.Now; //DateTime.Parse(Request.Form["notify_time"]);
                    Pan.Log.WriteLog(string.Format("ordernum:{0},buyerEmail:{1},tradeNo:{2},paydate:{3}", orderNum, buyerEmail, tradeNo, paydate));
                    int flag = (new Tuan.BLL.OrderInfo()).Update(orderNum,"2", "7", buyerEmail,tradeNo, paydate);
                    if (flag > 0)
                        Pan.Log.WriteLog(string.Format("数据库操作;订单号:{0},时间:{1} 数据库写入成功,支付金额:{2}", Request.QueryString["out_trade_no"], DateTime.Now.ToString(),total_fee), "TenPayReturn");
                    else
                        Pan.Log.WriteLog(string.Format("数据库操作;订单号:{0},失败时间:{1},通知重发，已作数据库处理，返回行数:{2}", Request.QueryString["out_trade_no"], DateTime.Now.ToString(), int.Parse(total_fee) / 100), "TenPayReturn");
                }
                catch (Exception dataEx)
                {
                    StringBuilder sbMeg = new StringBuilder("数据库操作出错了：" + DateTime.Now.ToString());
                    sbMeg.Append("; 订单号：");
                    sbMeg.Append(orderNum);
                    sbMeg.AppendLine(";数据库写入失败。");
                    sbMeg.AppendLine("错误消息：" + dataEx.Message);
                    sbMeg.AppendLine("错误方法：" + dataEx.TargetSite.ToString());
                    sbMeg.AppendLine("错误跟踪：" + dataEx.StackTrace);
                    Pan.Log.WriteLog(sbMeg.ToString(), "TenPayReturn");
                    //Response.Write("支付失败");
                    return;
                }
                string url = "../SucceedPay.aspx?payInterface=腾讯通&money=" +int.Parse(total_fee)/100+ "&NetPayMent=true&DB=true";
              
                resHandler.doShow(url);
            }
            else
            {
                //当做不成功处理
                Response.Write("支付失败");
            }

        }
        else
        {
            string debugInfo = resHandler.getDebugInfo();
            //Response.Write("<br/>debugInfo:" + debugInfo);
            Pan.Log.WriteLog("认证签名失败:"+debugInfo, "TenPayReturn");
            Response.Write("认证签名失败");
            string url = "../SucceedPay.aspx?payInterface=腾讯通&money=0&NetPayMent=false&DB=false&orderid=" + orderNum + "";

            
        }


    }
}
